package tq.com.tqcom.recyclerAdapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import tq.com.tqcom.recyclerAdapter.declear.IRecyclerItem;
import tq.com.tqcom.recyclerAdapter.declear.IRecyclerSection;

public abstract class BaseSectionAdapter<T extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<T> {
    protected ArrayList<IRecyclerSection> sections = new ArrayList<>();
    private int cacheCount = 0;

    @Override
    public int getItemCount() {
        int count = 0;
        if(cacheCount != 0) return cacheCount;

        synchronized (sections){
            for (IRecyclerSection s:
                    sections) {
                count += s.getiItemCount();
            }
            cacheCount = count;
        }
        return count;
    }

    @Override
    public int getItemViewType(int position) {
        IRecyclerItem item = getItemWithPos(position);
        return item.getType();
    }

    public int getItemPos(IRecyclerItem pItem){
        int pos = 0;
        boolean bFind = false;
        synchronized (sections){
            for (IRecyclerSection s:
                    sections) {
                int index = s.getItemPos(pItem);
                if(index != -1){
                    pos += index;
                    bFind = true;
                    break;
                }

                pos += s.getiItemCount();
            }
        }
        return bFind ? pos : -1;
    }

    protected IRecyclerItem getItemWithPos(int pos){
        IRecyclerItem item = null;
        int tmpPos = pos;
        synchronized (sections){
            for (IRecyclerSection s:
                    sections) {
                if(tmpPos < s.getiItemCount()){
                    return s.getItem(tmpPos);
                }else{
                    tmpPos -= s.getiItemCount();
                }
            }
        }

        return item;
    }

    protected IRecyclerSection getItemSectionWithPos(int pos){
        IRecyclerSection item = null;
        int tmpPos = pos;
        synchronized (sections){
            for (IRecyclerSection s:
                    sections) {
                if(tmpPos < s.getiItemCount()){
                    return s;
                }else{
                    tmpPos -= s.getiItemCount();
                }
            }
        }

        return item;
    }

    public void addSections(ArrayList<IRecyclerSection> pSections){
        if(pSections == null || pSections.size() == 0){
            return;
        }
        synchronized (sections){
            sections.addAll(pSections);
            cacheCount = 0;
        }
        notifyDataSetChanged();
    }
    public void addSection(IRecyclerSection section){
        int count = getItemCount();
        synchronized (sections){
            sections.add(section);
            cacheCount = 0;
        }
        notifyItemRangeChanged(count, section.getiItemCount());
    }

    public void insertSection(int pos, IRecyclerSection pSection){
        if(pSection == null) return;

        int tPos = 0;
        synchronized (sections){

            for (int i =0; i < sections.size(); i++){
                if(pos == i){
                    break;
                }

                tPos += sections.get(i).getiItemCount();
            }

            sections.add(pos, pSection);
            cacheCount = 0;
        }

        notifyItemRangeInserted(tPos, pSection.getiItemCount());
    }

    public void insertItem(IRecyclerSection pSection, IRecyclerItem pItem){
        if(pSection == null) return;

        int tPos = 0;
        synchronized (sections){

            for (int i =0; i < sections.size(); i++){
                if(sections.get(i) == pSection){
                    break;
                }

                tPos += sections.get(i).getiItemCount();
            }

            tPos += pSection.getiItemCount();

            pSection.addItem(pItem);
            cacheCount = 0;
        }

        notifyItemRangeInserted(tPos, 1);
    }

    public void clear(){
        synchronized (sections){
            sections.clear();
            cacheCount = 0;
        }
        notifyDataSetChanged();
    }

}
